#include "eeprom.h"

static void Delay10ms(void)	//@12.000MHz
{
	unsigned char data i, j;

	i = 117;
	j = 184;
	do
	{
		while (--j);
	} while (--i);
}

void write_val(unsigned char addr,unsigned char val)
{
	I2CStart();
	I2CSendByte(0xa0);
	I2CWaitAck();
	I2CSendByte(addr);
	I2CWaitAck();
	I2CSendByte(val);
	I2CWaitAck();
	I2CStop();
	Delay10ms();
}

unsigned char read_val(unsigned char addr)
{
	unsigned char read_val;
	I2CStart();
	I2CSendByte(0xa0);
	I2CWaitAck();
	I2CSendByte(addr);
	I2CWaitAck();
	I2CStart();
	I2CSendByte(0xa1);
	I2CWaitAck();
	read_val = I2CReceiveByte();
	I2CSendAck(1);
	I2CStop();
	return read_val;
}

void write_mutival(unsigned char addr,unsigned int val)
{
	I2CStart();
	I2CSendByte(0xa0);
	I2CWaitAck();
	I2CSendByte(addr*2);
	I2CWaitAck();
	I2CSendByte(val>>8);
	I2CWaitAck();
	I2CSendByte(val);
	I2CWaitAck();
	I2CStop();
	Delay10ms();
}
unsigned int read_mutival(unsigned int addr)
{
	unsigned int read_val;
	I2CStart();
	I2CSendByte(0xa0);
	I2CWaitAck();
	I2CSendByte(addr*2);
	I2CWaitAck();
	I2CStart();
	I2CSendByte(0xa1);
	I2CWaitAck();
	read_val = I2CReceiveByte();
	I2CSendAck(0);
	read_val = read_val << 8 | I2CReceiveByte();
	I2CSendAck(1);
	I2CStop();
	return read_val;
}
